package com.wd.mylibrary_login.activity


import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.tencent.mm.opensdk.constants.ConstantsAPI
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory

import com.wd.mylibrary_base.base.BaseActivity
import com.wd.mylibrary_login.R
import com.wd.mylibrary_login.databinding.ActivityStartBinding
import com.wd.mylibrary_login.viewmodel.LoginViewModel

@Route(path = "/login/Start")
class StartActivity : BaseActivity<LoginViewModel,ActivityStartBinding>(R.layout.activity_start) {

    override fun initView() {
        regToWx()

        findViewById<TextView>(R.id.Agreement).setOnClickListener {
            ARouter.getInstance().build("/login/agrement").navigation();

        }
        findViewById<TextView>(R.id.tong).setOnClickListener {
            ARouter.getInstance().build("/login/message").navigation();

        }
        dataBinding.ButtonDeng.setOnClickListener ( View.OnClickListener {
            ARouter.getInstance().build("/login/Deng").navigation();
        } )



        //微信登录
        dataBinding.Buttonid.setOnClickListener(View.OnClickListener {

            ARouter.getInstance().build("/main/MainActivity").navigation();

            val req = SendAuth.Req()
            req.scope = "snsapi_userinfo" // 只能填 snsapi_userinfo
            req.state = "wechat_sdk_demo_test"
            api.sendReq(req)
        })

    }

    override fun dataObserve() {

    }

    private val APP_ID = "wx064904aa8809f5aa"

    // IWXAPI 是第三方app和微信通信的openApi接口
    private lateinit var api: IWXAPI

    private fun regToWx() {
        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(this, APP_ID, true)

        // 将应用的appId注册到微信
        api.registerApp(APP_ID)

        //建议动态监听微信启动广播进行注册到微信
        registerReceiver(object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                // 将该app注册到微信
                api.registerApp(APP_ID)
            }
        }, IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP))
    }

}